package com.fl.util;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * @author liliang
 * 
 */
public class MailUtil {

	public static boolean isMailReceive(String from, String to,
			String mailContent, String subject, String mailHost,
			String userName, String passwd, String filePath) {

		try {

			Properties prop = new Properties();
			prop.put("mail.pop3.host", mailHost);

			Session session = Session.getDefaultInstance(prop);
			Store store = session.getStore("pop3");
			store.connect(mailHost, userName, passwd);
			Folder inbox = store.getDefaultFolder().getFolder("INBOX");
			inbox.open(Folder.READ_WRITE);
			Message[] msg = inbox.getMessages();

			int beforeSendMail = msg.length;

			sendMail(from, to, userName, passwd, mailHost, subject,
					mailContent, null);

			Thread.sleep(1000);

			Session session1 = Session.getDefaultInstance(prop);
			Store store1 = session1.getStore("pop3");
			store1.connect(mailHost, userName, passwd);
			Folder inbox1 = store1.getDefaultFolder().getFolder("INBOX");
			inbox1.open(Folder.READ_WRITE);
			Message[] msg1 = inbox1.getMessages();

			int afterSendMail = msg1.length;

			// System.out.println(beforeSendMail + ":" + afterSendMail);

			if (beforeSendMail + 1 != afterSendMail)
				return false;
			if (!subject.equals(msg1[afterSendMail - 1].getSubject()))
				return false;

			if (!StringUtil.isBlank(filePath)) {
				File file = new File(filePath);
				if (file.exists()) {
					Object content = msg1[afterSendMail - 1].getContent();
					if (!(content instanceof Multipart)) {
						return false;
					} else {
						Multipart mpt = (Multipart) content;
						if (mpt.getCount() == 0) {
							System.out.println("multipart is empty");
							return false;
						}
						Part pt = mpt.getBodyPart(1);
						if (!pt.getDisposition().equals(Part.ATTACHMENT)) {
							return false;
						}
						if (!pt.getFileName().equals(file.getName())) {
							return false;
						}
					}
				}
			}
			msg1[afterSendMail - 1].setFlag(Flags.Flag.DELETED, true);

			if (inbox != null) {

				inbox1.close(true);
			}
			if (store != null) {
				store.close();
			}
			if (store1 != null) {
				store1.close();
			}

		} catch (NoSuchProviderException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		} catch (InterruptedException e) {

			return false;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return true;
	}

	public static boolean sendMail(String from, String to, String username,
			String password, String host, String subject, String content,
			File file) {

		Properties props = new Properties();

		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");

		Session session = Session.getDefaultInstance(props);

		// session.setDebug(true);

		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
			message.setSubject(subject);
			Multipart multipart = new MimeMultipart();
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setText(content);
			multipart.addBodyPart(contentPart);

			// if (file != null) {
			// if (file.exists()) {
			// MimeBodyPart mbp = new MimeBodyPart();
			// URLDataSource uds = new URLDataSource(null);
			// mbp.setDataHandler(new DataHandler(uds));
			// mbp.setFileName("Log.xml");
			// multipart.addBodyPart(mbp);
			// }
			// }
			message.setContent(multipart);
			message.saveChanges();
			Transport transport = session.getTransport("smtp");
			transport.connect(host, username, password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
